package com.bmd.backgroundmanger.mapper;

import com.bmd.backgroundmanger.entity.PaperQuestion;
import com.bmd.backgroundmanger.entity.Question;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;

/**
 * <p>
 * 试卷题目关联 Mapper 接口
 * </p>
 *
 * @author 刷题App
 * @since 2024-12-19
 */
@Mapper
public interface PaperQuestionMapper extends BaseMapper<PaperQuestion> {

    /**
     * 根据试卷ID查询题目列表（包含题目详细信息）
     */
    @Select("SELECT pq.*, q.title, q.content, q.question_type, q.difficulty_level, " +
            "q.category_id, q.correct_answer, q.analysis, q.score " +
            "FROM paper_questions pq " +
            "LEFT JOIN questions q ON pq.question_id = q.question_id " +
            "WHERE pq.paper_id = #{paperId} " +
            "ORDER BY pq.question_order")
    List<PaperQuestion> selectByPaperIdWithQuestion(Integer paperId);

    /**
     * 根据试卷ID查询关联的题目列表
     */
    @Select("SELECT q.* FROM questions q " +
            "INNER JOIN paper_questions pq ON q.question_id = pq.question_id " +
            "WHERE pq.paper_id = #{paperId} " +
            "ORDER BY pq.question_order")
    List<Question> selectQuestionsByPaperId(Integer paperId);

    /**
     * 批量插入试卷题目关联
     */
    int insertBatch(List<PaperQuestion> paperQuestions);

    /**
     * 根据试卷ID删除关联
     */
    int deleteByPaperId(Integer paperId);

    /**
     * 获取试卷中题目的最大顺序号
     */
    @Select("SELECT COALESCE(MAX(question_order), 0) FROM paper_questions WHERE paper_id = #{paperId}")
    Integer getMaxOrderByPaperId(Integer paperId);
} 